package com.wx.tedu;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 29623
 */
public class DeleteContentBetweenMarkers {

    public static void main(String[] args) {
        ChangedTxt changed = new ChangedTxt();
        String filePath = changed.changedTxt();

        String keyword = "[抬杆到位] [0]";
        String line;

        List<String> dataBlocks = new ArrayList<>();
        StringBuilder blockBuilder = new StringBuilder();

        SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());

        File newFile =new File(changed.existsOutDirectory()+"//"+(formatter.format(date))+"_out.log");

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(newFile))) {
            System.out.println("\n程序开始文档过滤\n");
            while ((line = reader.readLine()) != null) {
                if (line.contains(keyword)) {
                    blockBuilder.append(line).append("\n");
                    if (blockBuilder.length() > 0) {
                        dataBlocks.add(blockBuilder.toString());
                    }
                    blockBuilder.setLength(0);
                } else {
                        blockBuilder.append(line).append("\n");
                }
            }
            if (blockBuilder.length() > 0) {//将文档最后不包括keyword数据块存放
                dataBlocks.add(blockBuilder.toString());
            }
            for (String block : dataBlocks) {
                if (!block.contains("交易失败") && !block.contains("余额不足") && !block.contains("黑名单卡") &&
                        !block.contains("非法") && !block.contains("交易异常") && !block.contains("未插卡") &&
                        !block.contains("重新插卡") && !block.contains("信息异常") && !block.contains("卡内无入口信息")) {
                    writer.write(block);
                    writer.flush();
                }
            }
            reader.close();
            writer.close();
            System.out.println("程序执行文档过滤完成！");
            System.out.println("文档过滤后文件所在位置："+newFile.getPath());
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage()+"此处截图！");
        }

    }

}
